let arr = [1, [1, [5, 6], 2], 3]
//利用some方法判断数组的是否存在一项数组 用while循环加concat方法一步一步扁平化
function solve1(arr) {
  while (arr.some(v => Array.isArray(v))) {
    arr = [].concat(...arr)
  }
  return arr
}
//将数组转化为字符串再转化为数组 最后将数组的每一项转化为整数
function solve2(arr) {
  return arr.join(',').split(',').map(v => parseInt(v))
}

//递归实现数组扁平化 
function sort3(arr) {
  var result = []
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      result = result.concat(sort3(arr[i]))
    } else {
      result.push(arr[i])
    }
  }
  return result
}
console.log(sort3(arr))

//实现对象的扁平化
let a = {
  obj: {
    name: 1,
    jack: 2,
    y: {
      b: 3,
      d: 6
    }
  }
}
function bat(obj) {
  let result = {}
  for (let i in obj) {
    if (typeof (obj[i]) == 'object') {
      let t = bat(obj[i])
      for (let j in t) {
        t[i + '.' + j] = t[j]
        delete t[j]
      }
      result = Object.assign(result, t)
    } else {
      result[i] = obj[i]
    }
  }
  return result
}
console.log(bat(a))

